package cn.edu.util;

import cn.edu.entity.City;
import cn.edu.entity.Province;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class CityUtil {

    public static List<City> getList(String sql, Object... objs)throws Exception{
        List<City> list = new ArrayList<>();
        Connection conn = DBUtil.getConnection();
//         "select * from tb_user where name=? and password=?";
        PreparedStatement prestat = conn.prepareStatement(sql);
        if(objs!=null){
            for (int i = 0; i < objs.length; i++) {
                prestat.setObject(i+1,objs[i]);//eitity实体类定义数据,建议都是包装类.
            }
        }
        ResultSet rs = prestat.executeQuery();
        while(rs.next()){
            City city = new City();
            city.setId((Integer) rs.getObject(1));
            city.setName((String) rs.getObject(2));
            city.setpId((Integer) rs.getObject(3));
            list.add(city);
        }
        DBUtil.close(conn,prestat,rs);
        return list ;
    }
}
